#include "logindialog.h"
#include "ui_logindialog.h"
#include "friendlist.h"
#include "register.h"
#include <QMessageBox>
#include <QCheckBox>
#include <QDebug>
#include <QPixmap>
#include <QMouseEvent>
#include <QJsonDocument>
#include <QJsonObject>


LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);

    tcpSocket2=new QTcpSocket(this);
    tcpSocket2->connectToHost("localhost",8022);
    connect(tcpSocket2, &QTcpSocket::connected, this, &LoginDialog::onConnected);
    connect(tcpSocket2, &QTcpSocket::disconnected, this, &LoginDialog::onDisconnected);
    connect(tcpSocket2, &QTcpSocket::readyRead, this, &LoginDialog::onReadyRead);

    //输入提示语句
    if(ui->acc_value->text().isEmpty()){
        ui->acc_value->setPlaceholderText(tr("账号"));
    }
    else{
        ui->acc_value->setPlaceholderText(QString::null);
    }
    if(ui->paw_value->text().isEmpty()){
        ui->paw_value->setPlaceholderText(tr("密码"));
    }
    else{
        ui->paw_value->setPlaceholderText(QString::null);
    }
    QPixmap *pixmap1=new QPixmap(":/new/pr/resource/login/account.jpg");
    pixmap1->scaled(ui->account->size(),Qt::KeepAspectRatio);
    ui->account->setScaledContents(true);
    ui->account->setPixmap(*pixmap1);
    QPixmap *pixmap2=new QPixmap(":/new/pr/resource/login/password.jpg");
    pixmap1->scaled(ui->password->size(),Qt::KeepAspectRatio);
    ui->password->setScaledContents(true);
    ui->password->setPixmap(*pixmap2);
    ui->acc_value->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
    ui->paw_value->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
    ui->regis_btn->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
    ui->login_btn->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::on_login_btn_clicked()
{
    QString str1=ui->acc_value->text();
    QString str2=ui->paw_value->text();

    //创建QJson数据类型，并向服务器端发送
    QJsonObject dataObject2;
    dataObject2["sendtype"]="login";
    dataObject2["selfaccount"]=str1;
    dataObject2["selfpassword"]=str2;
    QJsonDocument dataDocument2(dataObject2);
    QByteArray jsonData2=dataDocument2.toJson();
    tcpSocket2->write(jsonData2);
}

void LoginDialog::on_checkBox_stateChanged(int arg1)
{
    switch(arg1){
    case 0:
        ui->paw_value->setEchoMode(QLineEdit::Password);
        break;
    case 2:
        ui->paw_value->setEchoMode(QLineEdit::Normal);
        break;
    }
}

void LoginDialog::on_regis_btn_clicked()
{
    Register* _register=new Register;
    _register->show();
}

//解决窗口不能移动的问题
void LoginDialog::mousePressEvent(QMouseEvent *event)
{
    this->windowPos = this->pos();       // 获得部件当前位置
    this->mousePos = event->globalPos(); // 获得鼠标位置
    this->dPos = mousePos - windowPos;   // 移动后部件所在的位置
}

void LoginDialog::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - this->dPos);
}

//页面最小化和关闭
void LoginDialog::on_toolButton_clicked()
{
    this->showMinimized();
}

void LoginDialog::on_exit_btn_clicked()
{
    this->close();
}

void LoginDialog::onConnected(){
    qDebug()<<"LoginDialog onConnected"<<endl;
}

void LoginDialog::onDisconnected(){
    qDebug()<<"LoginDialog onDisconnected"<<endl;
}

void LoginDialog::onReadyRead(){
    QByteArray jsonData2=tcpSocket2->readAll();
    QJsonDocument dataDocument2=QJsonDocument::fromJson(jsonData2);
    if(!dataDocument2.isNull()&&dataDocument2.isObject()){
        QJsonObject dataObject2=dataDocument2.object();
        if(dataObject2["returntype"].toString()=="login"){
            if(dataObject2["isLogin"].toBool()){
                friendList *f=new friendList;
                f->show();
                f->setselfInformation(dataObject2["selfaccount"].toString(),dataObject2["selfname"].toString(),dataObject2["selfpic_path"].toString(),dataObject2["selfsignature"].toString());
                tcpSocket2->abort();
                this->close();
            }
            else{
                ui->label_2->setText(tr("账号或密码不正确，请重新输入！"));
            }
        }
    }
}

void LoginDialog::acc_changed(){
    ui->label_2->clear();
}
